package com.example.messagedigest;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MainActivity extends AppCompatActivity {

    private String pw="softpo123";
    //浏览器MD5运算的结果：6a2ac3dd2d6b159ab9055c5c75e16089
    //浏览器SHA-1运算的结果：990f8b6e7990ff360070c9efe53709ef7c370ffd

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    //button的点击事件，进行运算
    public void messageDigest(View view) {
        switch (view.getId()) {
            case R.id.md5://32位
                String md5 = messageDigest("MD5", pw);
                Log.d("1608", "---------------->messageDigest: 经过MD5运算后的结果"+md5);
                break;
            case R.id.sha1://40位
                String sha1 = messageDigest("SHA-1", pw);
                Log.d("1608", "---------------->messageDigest: 经过SHA-1运算后的结果"+sha1);
                break;
        }
    }

    //消息摘要的运算方法，就创建了
    private String messageDigest(String type, String data) {

        try {
            //1、创建消息摘要的对象
            MessageDigest messageDigest=MessageDigest.getInstance(type);

            //2、调用对象的方法，进行运算

            //将要运算的数据加载给MessageDigest
            messageDigest.update(data.getBytes());

            //运算之后的数据是一个哈希值
            byte[] digest = messageDigest.digest();

            //3、将字节数组转换成该样式：5d41402abc4b2a76b9719d911017c592
            //5d41402abc4b2a76b9719d911017c592
            //5a 53 e4 cb c5 ba 32 b8 a5 92 f2 03 23 1d cd 96
            //规律：0到9，a到f;十六进制表示数据

            //3.1 打印运算后的数据
//            Log.d("1608",
//                    "--------------->messageDigest:hash值得字节数组长度 "
//                            +digest.length);

            for (int i = 0; i < digest.length; i++) {
//                Log.d("1608", "---------------->messageDigest: "+digest[i]);

//                Log.d("1608", "---------------->messageDigest: 二进制"+Integer.toBinaryString(digest[i]));
            }

            /*
            11-24 22:32:24.607 29841-29841/com.example.messagedigest D/1608: --------------->messageDigest:hash值得字节数组长度 16
            11-24 22:32:24.615 29841-29841/com.example.messagedigest D/1608: ---------------->messageDigest: 二进制1101010
            11-24 22:32:24.615 29841-29841/com.example.messagedigest D/1608: ---------------->messageDigest: 二进制101010
            11-24 22:32:24.615 29841-29841/com.example.messagedigest D/1608: ---------------->messageDigest: 二进制11111111111111111111111111000011
            11-24 22:32:24.615 29841-29841/com.example.messagedigest D/1608: ---------------->messageDigest: 二进制11111111111111111111111111011101
            11-24 22:32:24.615 29841-29841/com.example.messagedigest D/1608: ---------------->messageDigest: 二进制101101
            11-24 22:32:24.615 29841-29841/com.example.messagedigest D/1608: ---------------->messageDigest: 二进制1101011
            11-24 22:32:24.615 29841-29841/com.example.messagedigest D/1608: ---------------->messageDigest: 二进制10101
            11-24 22:32:24.615 29841-29841/com.example.messagedigest D/1608: ---------------->messageDigest: 二进制11111111111111111111111110011010
            11-24 22:32:24.615 29841-29841/com.example.messagedigest D/1608: ---------------->messageDigest: 二进制11111111111111111111111110111001
            11-24 22:32:24.615 29841-29841/com.example.messagedigest D/1608: ---------------->messageDigest: 二进制101
            11-24 22:32:24.615 29841-29841/com.example.messagedigest D/1608: ---------------->messageDigest: 二进制1011100
            11-24 22:32:24.615 29841-29841/com.example.messagedigest D/1608: ---------------->messageDigest: 二进制1011100
            11-24 22:32:24.615 29841-29841/com.example.messagedigest D/1608: ---------------->messageDigest: 二进制1110101
            11-24 22:32:24.615 29841-29841/com.example.messagedigest D/1608: ---------------->messageDigest: 二进制11111111111111111111111111100001
            11-24 22:32:24.615 29841-29841/com.example.messagedigest D/1608: ---------------->messageDigest: 二进制1100000
            11-24 22:32:24.615 29841-29841/com.example.messagedigest D/1608: ---------------->messageDigest: 二进制11111111111111111111111110001001

             */

            //需要将字节数组转换成16进行表示
            
            //二进制 与运算

            StringBuilder builder = new StringBuilder();

            for (int i = 0; i < digest.length; i++) {
                //0xFF = 二进制：1111 1111 ==0xFF
                //F == 15 二进制 1111 =15
                int num = digest[i] & 0xFF;
                
                if (num<=0xF){//或者num<16
                    builder.append(0);
                }
                //toBinaryString:二进制
                //toHexString:十六进制
                String result = Integer.toHexString(num);
                
                builder.append(result);
            }
            
            //运算后的数据，返回
            return builder.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        return null;
    }
}
